﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Xml.Linq;
using System.Web;
using System.Web.Services;
using DAL;

namespace WebService
{
    /// <summary>
    /// Summary description for Expenses
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Expenses : System.Web.Services.WebService
    {
        [WebMethod]
        //Recebe um expense type, se esse id corresponder a um tipo "em uso" devolve apenas esse, caso contrário
        //Retorna uma lista de tipos de despesa com as mesmas opções que o passado por parametro
        public List<ShortExpenseType> GetAvailableTypes(String mail, String ExpenseTypeID)
        {
            List<ShortExpenseType> list = new List<ShortExpenseType>();
            ExpenseType expense_wanted;
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                var query = from expTypes in dc.ExpenseTypes
                            where expTypes.Id.Equals(new Guid(ExpenseTypeID))
                            select expTypes;

                expense_wanted = query.Single<ExpenseType>();
            }

            if (true)// expense_wanted.depreecated) <- ou outro controlo de versões
            {
                ShortExpenseType wanted = new ShortExpenseType();
                wanted.guid = ExpenseTypeID;
                wanted.name = expense_wanted.Name;
                list.Add(wanted);
            }
            else
            {
                //procurar despesas "iguais" e por na lista
            }


            return list;
        }

        [WebMethod]
        //Cria um Report e devolve o seu id para uso nos outros serviços
        public String CreateNewReport(String mail, String pass, String name, String comments)
        {
            ExpenseReport expenseReport = new ExpenseReport();
            expenseReport.Name = name;
            expenseReport.Comments = comments;

            expenseReport.UserEmail = mail;
            //check pass

            Guid id = Guid.NewGuid();
            expenseReport.Id = id;
            expenseReport.CreationDate = DateTime.Now;

            //submit para a DB
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                dc.ExpenseReports.InsertOnSubmit(expenseReport);
                dc.SubmitChanges();
            }
            return id.ToString();
        }

        [WebMethod]
        //Cria uma linha e adiciona-a ao report
        public Boolean CreateNewLine(String mail, String pass, String reportID, String expenseTypeID, IShortExpenseData data)//, ExpenseLine line)
        {
            return false;
        }

        [WebMethod]
        //Fecha o report e avisa o workflow
        public Boolean CloseReport(String mail, String pass, String reportID)
        {
            return false;
        }

        [WebMethod]
        //Retorna os ultimos N reports do utilizador assim como o seu estado (EmEspera/Aprovado/ETC)
        public String[] GetMyLastReports(String mail, String pass, int nrOfReports)
        {
            return null;
        }


        
    }
}
